// 关于登录
const id = window.localStorage.getItem('id')
const token = window.localStorage.getItem('token')
const onBox = document.querySelector('.on')
const offBox = document.querySelector('.off')

// 登录
if (!id) {
    onBox.classList.remove('active')
    offBox.classList.add('active')
}
const xhr = new XMLHttpRequest()
xhr.open('GET', 'http://localhost:8888/users/info?id=' + id)
xhr.onload = function () {
    const res = JSON.parse(xhr.responseText)
    if (res.code === 401) {
        onBox.classList.remove('active')
        offBox.classList.add('active')
    }
    if (res.code === 1) {
        onBox.firstElementChild.innerText = res.info.nickname
        offBox.classList.remove('active')
        onBox.classList.add('active')
    }
}

xhr.setRequestHeader('authorization', token)
xhr.send()

// 搜索引擎
const inp = document.querySelector('input')
const ulBox = document.querySelector('ul')

// 给 input 绑定 输入事件 
inp.oninput = function () {
    // 获取输入的内容
    const text = inp.value.trim()
    // jsonp
    const script = document.createElement('script')
    script.src = `https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=35105,31660,35239,35049,34584,34505,34872,35320,26350,35209,35147,22157&wd=${text}&req=2&csor=1&cb=handler&_=1638241997884`
    document.body.appendChild(script)
    script.remove()
}
function handler(res) {
    if(!res.g){
        ulBox.style.display = 'none'
        return
    }
    let str=''
    res.g.forEach(item =>{
        str +=`
         <li>${item.q}</li>
        `
    })
    ulBox.innerHTML = str
    ulBox.style.display = 'block'
}